home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / database / msgobj10.zip / SMALLPIP.CPP < prev    next >
C/C++ Source or Header  |  1993-03-13  |  4KB  |  134 lines

  1. // SMALLPIP.CPP: a little message reader
  2.  
  3. #include <conio.h>
  4. #include <ctype.h>
  5. #include <fstream.h>
  6. #include <iostream.h>
  7. #include <alloc.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #define MAIN
  12. #include "pipbase.h"
  13. #include "pipreply.h"
  14. #include "pipext.h"
  15. #include "config.h"
  16. #include "msgobj.h"
  17.  
  18.  
  19. void main(int argc,char *argv[])
  20. {
  21.   FILE *f;
  22.   int msgnum,k;
  23.   char fn[72],*p;
  24.   if(argc!=2)
  25.     {
  26.       cerr << "usage: SMALLPIP area#\n";
  27.       exit(1);
  28.     }
  29.   // reads configuration files created by PipBase/Gimme-a-Point/PipSetup/Pip*
  30.   f=fopen("NodeInfo.Cfg","rb");
  31.   if(!f)
  32.     {
  33.       cerr << "I cannot find NodeInfo.Cfg\nplease create it with PIP SET\n";
  34.       exit(1);
  35.     }
  36.   fread(&cfg,sizeof cfg,1,f);
  37.   fclose(f);
  38.   // allocates a message object
  39.   message_frame msg;
  40.   // sets message area
  41.   msg.area=atoi(argv[1]);
  42.   // allocates a buffer for message text
  43.   if (coreleft()<24000)
  44.     {
  45.       cerr << "Not enough memory!\n";
  46.       exit(1);
  47.     }
  48.   if(msg.allocate_text((coreleft()>BUFSIZE+20000)?BUFSIZE:(coreleft()-20000)))
  49.     {
  50.       cerr << "Allocation error\n";
  51.       exit(1);
  52.     }
  53.   cout << "Message buffer: " << msg.textsize << " bytes\n";
  54.   // opens lastread pointers (this operation is not currently implemented in
  55.   // MessageObject
  56.   sprintf(fn,"%sLastRead.Pip",cfg.pipdir);
  57.   f=fopen(fn,"rb");
  58.   fseek(f,msg.area*sizeof(msgnum),SEEK_SET);
  59.   fread(&msgnum,sizeof msgnum,1,f);
  60.   fclose(f);
  61.   cout << "lastread=" << msgnum << "\n";
  62.   k=0;
  63.   while(k!=27)
  64.     {
  65.       // to read a message, assign msg.area and msg.msg_number...
  66.       msg.msg_number=msgnum;
  67.       // ... and then issue a msg.read();
  68.       if(msg.read())
  69.         {
  70.           cout << "Problems in accessing message #" << msgnum << " (" << msg.lasterror << ")\n";
  71.           cout << "msg.area=" << msg.area << " msg.msg_number=" << msg.msg_number << "\n";
  72.         }
  73.       else
  74.         {
  75.           // prints message header
  76.           cout << "Message # " << msgnum << "\n";
  77.           msg.fromaddr.pack(fn);
  78.           cout << "From: " << msg.from << " (" << fn << ")\n";
  79.           msg.toaddr.pack(fn);
  80.           cout << "To:   " << msg.to << " (" << fn << ")\n";
  81.           cout << "Subj: " << msg.subject << "\n";
  82.           // msg.text is stored as-is: a little operations are required to
  83.           // restore LineFeeds, skip kludges, etc.
  84.           p=msg.text;
  85.           while(*p)
  86.             {
  87.               switch((unsigned char)*p)
  88.                 {
  89.                   case 1:
  90.                     while(*(p+1) && (*(p+1)!=10) && (*(p+1)!=13) && ((unsigned char)*(p+1)!=141))
  91.                       p++;
  92.                     break;
  93.                   case 10:
  94.                   case 13:
  95.                   case 141:
  96.                     cout << "\n";
  97.                     break;
  98.                   default:
  99.                     cout << *p;
  100.                 }
  101.               p++;
  102.             }
  103.         }
  104.       // nothing particular...
  105.       cout << "Command? Esc=quit; -=previous; +=Next; R=repeat >";
  106.       k=toupper(getch());
  107.       cout << "\n";
  108.       switch(k)
  109.         {
  110.           case '+':
  111.             if(msgnum<msg.highest_message())
  112.               msgnum++;
  113.             else
  114.               {
  115.                 cout << "Messages ended.\n";
  116.                 k=27;
  117.               }
  118.             break;
  119.           case '-':
  120.             if(msgnum) msgnum--;
  121.             break;
  122.           case 'R':
  123.             break;
  124.         }
  125.     }
  126.   sprintf(fn,"%sLastRead.Pip",cfg.pipdir);
  127.   f=fopen(fn,"rb+");
  128.   fseek(f,msg.area*sizeof(msgnum),SEEK_SET);
  129.   fwrite(&msgnum,sizeof msgnum,1,f);
  130.   fclose(f);
  131.   // files used by MessageObject are closed automatically by
  132.   // message_frame::~message_frame (the destructor)
  133. }
  134.